In [1]:
#Adapted from https://pythonprogramming.net/flat-clustering-machine-learning-python-scikit-learn/
from sklearn.cluster import KMeans
import numpy as np
In [2]:
data = np.array([[1, 2],
[5, 8],
[1.5, 1.8],
[8, 8],
[1, 0.6],
[9, 11]])
In [3]:
kmeans = KMeans(n_clusters=2)
kmeans.fit(data)
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
print(centroids)
print(labels)
In [4]:
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
%matplotlib inline
In [7]:
colors = ["g.","r.","c.","y."]
for i in range(len(data)):
print("coordinate:",data[i], "label:", labels[i])
plt.plot(data[i][0], data[i][1], colors[labels[i]], markersize = 10)
plt.scatter(centroids[:, 0],centroids[:, 1], marker = "x", s=150, linewidths = 5, zorder = 10)
plt.show()
In [ ]: